package com.swz.dcrm.adpter;


import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.swz.dcrm.R;
import com.swz.dcrm.base.ViewHolder;
import com.swz.dcrm.model.CarModel;

import com.swz.dcrm.util.FileUtils;
import com.swz.dcrm.util.Tool;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class CarShopCarModelAdapter extends CustomAdapter<CarModel> {

    private boolean edit;
    private boolean selectAll;
    private OnCollectClickListener onCollectClickListener;
    private Map<Integer, CarModel> modelStateMap = new HashMap<>();


    AbsoluteSizeSpan absoluteSizeSpan11;
    AbsoluteSizeSpan absoluteSizeSpan16;
    ForegroundColorSpan foregroundColorSpanlight;
    ForegroundColorSpan foregroundColorSpanDark;

    public interface OnCollectClickListener {
        void onClick(int position, ImageView imageView);
    }

    public CarShopCarModelAdapter(Context context, List<CarModel> datas) {
        super(context, R.layout.item_car_model_state, datas);
        absoluteSizeSpan11=new AbsoluteSizeSpan(Tool.dip2px(mContext,11));
        absoluteSizeSpan16=new AbsoluteSizeSpan(Tool.dip2px(mContext,16));
        foregroundColorSpanlight=new ForegroundColorSpan(Color.parseColor("#ff8730"));
        foregroundColorSpanDark=new ForegroundColorSpan(Color.parseColor("#6f777d"));
    }

    @Override
    protected void convert(ViewHolder holder, CarModel carModelState, int position) {

        int move = Tool.dip2px(mContext, 35);
        ConstraintLayout c = holder.getView(R.id.c);
        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) c.getLayoutParams();
        ValueAnimator valueAnimator;
        if (edit && (layoutParams.leftMargin == 0)) {
            valueAnimator = ValueAnimator.ofInt(0, move);
            valueAnimator.addUpdateListener(animation -> {

                layoutParams.setMargins((Integer) animation.getAnimatedValue(), 0, 0, 0);
                c.setLayoutParams(layoutParams);
            });
            valueAnimator.setDuration(300);
            valueAnimator.start();
            holder.getView(R.id.iv_collect).setVisibility(View.GONE);

        } else if (!edit && (layoutParams.leftMargin == move)) {
            valueAnimator = ValueAnimator.ofInt(move, 0);
            valueAnimator.addUpdateListener(animation -> {
                layoutParams.setMargins((Integer) animation.getAnimatedValue(), 0, 0, 0);
                c.setLayoutParams(layoutParams);
            });
            valueAnimator.setDuration(300);
            valueAnimator.start();
            holder.getView(R.id.iv_collect).setVisibility(View.VISIBLE);
        }
        CheckBox checkBox=holder.getView(R.id.checkbox);
        checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
           if(isChecked){
               modelStateMap.put(position,carModelState);
           }else {
               modelStateMap.remove(position);
           }
        });
        Glide.with(mContext)
                .load(R.mipmap.car_default)
                .into((ImageView) holder.getView(R.id.iv_car));
         checkBox.setChecked(selectAll);

        holder.setText(R.id.tv_car_model, carModelState.getDesc());

        String str=mContext.getString(R.string.item_way, carModelState.getInStockCount());
        SpannableString spannableString = new SpannableString(str);

        spannableString.setSpan(foregroundColorSpanDark, 0,1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(absoluteSizeSpan11,0,1,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if(carModelState.getInStockCount()==0){
            spannableString.setSpan(foregroundColorSpanlight, 2,str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }else {
            spannableString.setSpan(foregroundColorSpanDark, 2,str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        spannableString.setSpan(absoluteSizeSpan16,2,str.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ((TextView)holder.getView(R.id.tv_way)).setText(spannableString);
        str=mContext.getString(R.string.item_inventory, carModelState.getInStockCount());
        spannableString = new SpannableString(str);
        spannableString.setSpan(foregroundColorSpanDark, 0,1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(absoluteSizeSpan11,0,1,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if(carModelState.getOnPassageCount()==0){
            spannableString.setSpan(foregroundColorSpanlight, 2,str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }else {
            spannableString.setSpan(foregroundColorSpanDark, 2,str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        spannableString.setSpan(absoluteSizeSpan16,2,str.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ((TextView)holder.getView(R.id.tv_inventory)).setText(spannableString);
        if (carModelState.isBeCollected()) {
            Glide.with(mContext)
                    .load(R.mipmap.star_active)
                    .into((ImageView) holder.getView(R.id.iv_collect));
        } else {
            Glide.with(mContext)
                    .load(R.mipmap.star)
                    .into((ImageView) holder.getView(R.id.iv_collect));
        }




        holder.setOnClickListener(R.id.iv_collect,v -> {
            setCurrentClickPosition(position);
            if (onCollectClickListener != null&&!carModelState.isBeCollected()) {
                onCollectClickListener.onClick(position, (ImageView) v);
            }
        });
        holder.itemView.setOnClickListener(v -> {
            setCurrentClickPosition(position);
            if(onClickListener!=null){
                onClickListener.onItemClick(carModelState);
            }
        });
    }

    @Override
    public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {

        super.onDetachedFromRecyclerView(recyclerView);
    }

    public void edit(boolean finish) {
        edit = !finish;
        notifyDataSetChanged();
    }

    public List<CarModel>getSelectedList(){
        return  new ArrayList<>(modelStateMap.values());
    }

    public void selectAll(boolean b){
        selectAll=b;
        notifyDataSetChanged();
    }
    public void removeList(){
        List<CarModel> list=  new ArrayList<>(modelStateMap.values()) ;
        List<Integer> position=  new ArrayList<>(modelStateMap.keySet()) ;
        if(list!=null&&list.size()>0){

            for(int i=0;i<list.size();i++){
                modelStateMap.remove(position.get(i));
                mDatas.remove(list.get(i));
                notifyItemRemoved(position.get(i));
            }
          notifyItemRangeChanged(0,mDatas.size());
        }
    }
    public void setOnCollectClickListener(OnCollectClickListener onCollectClickListener) {
        this.onCollectClickListener = onCollectClickListener;
    }
}
